<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="搜索供应商名称">
        <el-input v-model="sjMerchant.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-button round type="primary" icon="el-icon-search" @click="getList()"
        >搜索</el-button
      >
      <el-button round type="danger" icon="el-icon-delete" @click="clear"
        >清空</el-button
      >
    </el-form>
    <!-- 表单显示数据 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column label="序号" width="200">
        <template slot-scope="scope">
          {{ (current - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="供应商名称" width="300">
      </el-table-column>
      <el-table-column label="logo" width="300">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="gmtCreate" label="注册时间" width="400">
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="200"> </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="`/merchant/edit/` + scope.row.id">
            <el-button size="mini" round type="success" icon="el-icon-edit">
              修改</el-button
            >
          </router-link>
          <el-button
            size="mini"
            round
            type="danger"
            icon="el-icon-delete"
            @click="removeDataById(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      layout="total, prev, pager, next, jumper"
      style="padding: 30px 0; text-align: center"
      @current-change="getList"
    >
    </el-pagination>
  </div>
</template>

<script>
import merchant from "@/api/merchant/merchant";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { merchant },
  data() {
    //这里存放数据
    return {
      list: null, // 显示的所有数据
      current: 1, // 当前页
      limit: 5, // 每页记录数
      total: 0, // 总记录数
      sjMerchant: {}, // 条件封装对象，只需要传name值
    };
  },
  //方法集合
  methods: {
    // 获取页面
    getList(current = 1) {
      this.current = current;
      merchant
        .getMerchantListPage(this.current, this.limit, this.sjMerchant)
        .then((request, response) => {
          this.list = request.data.rows;
          this.total = request.data.total;
        });
    },
    // 清空查询数据
    clear() {
      this.sjMerchant = {};
      this.getList();
    },
    // 修改
    // edit(sjMerchant) {
    //   console.log(sjMerchant);
    // },
    // 删除
    removeDataById(sjMerchant) {
      this.$confirm(`是否删除${sjMerchant.name}供应商`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          merchant.removeDataById(sjMerchant.id).then((response) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch();
    },
  },
  //生命周期 - 创建完成
  created() {
    this.getList();
  },
};
</script>
<style scoped>
</style>